package com.baohulu.baohulu.utils;

import android.content.Context;

import com.baohulu.baohulu.MyApplication;
import com.baohulu.baohulu.common.Const;

public class SharedPrefUtil {

    public static void putLong(String key, long value) {
        MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .edit()
                .putLong(key, value)
                .apply();
    }

    public static void putInt(String key, int value) {
        MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .edit()
                .putInt(key, value)
                .apply();
    }

    public static void putDouble(String key, double value) {
        MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .edit()
                .putString(key, String.valueOf(value))
                .apply();
    }

    public static String getString(String key) {
        return MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .getString(key, "");
    }

    public static boolean getBoolean(String key) {
        return MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .getBoolean(key, false);
    }

    public static long getLong(String key) {
        return MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .getLong(key, 0L);
    }

    public static int getInt(Context context, String key) {
        return MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .getInt(key, 0);
    }

    public static double getDouble(Context context, String key) {
        String str = MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .getString(key, "");
        return Double.valueOf(str);
    }

    public static void clearLoginState() {
        putBoolean(Const.SpKey.IS_LOGIN, false);
        putString(Const.SpKey.LOGIN_USER_ACCOUNT, "");
        putString(Const.SpKey.LOGIN_USER_NICKNAME, "");
    }

    public static void putBoolean(String key, boolean value) {
        MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .edit()
                .putBoolean(key, value)
                .apply();
    }

    public static void putString(String key, String value) {
        MyApplication.getAppContext().getSharedPreferences(Const.SP_NAME, Context.MODE_PRIVATE)
                .edit()
                .putString(key, value)
                .apply();
    }
}
